package com.icebergteam.warfacewidget.ui.configuration;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.settings.GeneralSettings;
import com.icebergteam.warfacewidget.news.ui.NewsActivity;
import com.icebergteam.warfacewidget.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class PrefsGeneralFragment extends Fragment implements PrefsFragment {
	public static final String className = NewsActivity.class.getCanonicalName();
	//
	public static final int FRAGMENT_ID = 0;
	//
	GeneralSettings generalSettings;
	LinearLayout prefsContainer;
	// Active views
//	CheckBox	serverAutodetect;
//	TextView	serverRegion;
//	TextView	serverLanguage;
	TextView	uiLanguage;
	Intent intent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			generalSettings = AppManager.getGeneralSettings();
			intent = getActivity().getIntent();
		} catch (Exception e) {
			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	
		ScrollView mainView = (ScrollView)inflater.inflate(R.layout.prefs_template_layout, container, false);
		prefsContainer = (LinearLayout) mainView.findViewById(R.id.prefs_main_layout);
		//prefsContainer.addView(inflater.inflate(R.layout.prefs_general_server_layout, prefsContainer, false)); // temp
		prefsContainer.addView(inflater.inflate(R.layout.prefs_general_ui_layout, container, false));
		
		setViews();
		try {
			setValues();
		} catch (Exception e) {
			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
		}
		
		return mainView;
	}
	
	@Override
	public void setViews()	{
//		serverAutodetect	= (CheckBox) prefsContainer.findViewById(R.id.prefs_general_server_autodetect_checkBox);
//		serverRegion		= (TextView) prefsContainer.findViewById(R.id.prefs_general_server_region_textView);
//		serverLanguage		= (TextView) prefsContainer.findViewById(R.id.prefs_general_server_language_textView);
		uiLanguage			= (TextView) prefsContainer.findViewById(R.id.prefs_general_ui_language_textView);
	}

	@Override
	public void setValues() throws Exception {
//		// Server autodetect
//		serverAutodetect.setChecked(generalSettings.isAutodetectRegion());
//		// 
//		serverRegion.setText(generalSettings.getServerRegion());
//		serverLanguage.setText(generalSettings.getServerLang());
		// UI
		uiLanguage.setText(AppManager.getLocaleManager().getCurrentUILang());
		//
		blockDependedViews();
		
		AppManager.getGeneralSettings().save();
	}
	
	@Override
	public boolean checkFields(View v) throws Exception	{
		ConfigurationActivity.startTapFieldAnimation(v);
		switch(v.getId())	{
		// Server autodetect
		case R.id.prefs_general_server_autodetect_field:
			setServerAutodetection();
			break;
		// Server region
		case R.id.prefs_general_server_region_field:
			showSelectServerRegionDialog();
			break;
		// Server language
		case R.id.prefs_general_server_language_field:
			showSelectServerLanguageDialog();
			break;
		case R.id.prefs_general_ui_language_field:
			showSelectUILanguageDialog();
			break;
		default:
			return false;
		}
		setValues();
		return true;
	}
	
	private void blockDependedViews()	{
//		// Block depended views
//		View v;
//		// Region
//		v = (View)prefsContainer.findViewById(R.id.prefs_general_server_region_field);
//		v.setEnabled(!generalSettings.isAutodetectRegion());
//		v.setVisibility(!generalSettings.isAutodetectRegion() ? View.VISIBLE : View.GONE);
//		// Region lang
//		v = (View)prefsContainer.findViewById(R.id.prefs_general_server_language_field);
//		v.setEnabled(!generalSettings.isAutodetectRegion());
//		v.setVisibility(!generalSettings.isAutodetectRegion() ? View.VISIBLE : View.GONE);
	}
	
	// Server autodetect
	private void setServerAutodetection() throws Exception	{
//		generalSettings.setAutodetectRegion(!generalSettings.isAutodetectRegion());
//		// Autodetect server
//		try {
//			if (generalSettings.isAutodetectRegion()) {
//				AppManager
//						.getLocaleManager()
//						.setupServerLocaleSettings();
//			}
//		} catch (Exception e) {
//			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
//		}
	}
	
	// Server region
	private void showSelectServerRegionDialog() throws Exception	{
//		AlertDialog.Builder ad = new AlertDialog.Builder(getActivity())
//			.setTitle(R.string.prefs_general_group_server_region_desc)
//			.setCancelable(true)
//		// Add items
//			.setSingleChoiceItems(AppManager.getLocaleManager().getRegions(), 
//					AppManager.getLocaleManager().getCurrentRegionIndex(), 
//					new OnClickListener() {
//			
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					try {
//						generalSettings.setServerRegionById(which);
//						setValues();
//					} catch (Exception e) {
//						LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
//						e.printStackTrace();
//					}
//					
//					dialog.cancel();
//				}
//			})
//		// Add buttons
//			.setNegativeButton("Cancel", new OnClickListener()	{
//	
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					dialog.cancel();
//				}
//			});
//		// Show
//		ad.show();
	}
	// Server language	
	private void showSelectServerLanguageDialog() throws Exception	{
//		AlertDialog.Builder ad = new AlertDialog.Builder(getActivity())
//			.setTitle(R.string.prefs_general_group_server_language_desc)
//			.setCancelable(true)
//		// Add items
//			.setSingleChoiceItems(AppManager.getLocaleManager().getCurrentRegionLangs(), 
//					AppManager.getLocaleManager().getCurrentServerLangIndex(),
//					new OnClickListener() {
//			
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					try {
//						generalSettings.setServerLang(AppManager.getLocaleManager().getCurrentRegionLangs()[which]);
//						setValues();
//					} catch (Exception e) {
//						LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
//						e.printStackTrace();
//					}
//					dialog.cancel();					
//				}
//			})
//		// Add buttons
//			.setNegativeButton("Cancel", new OnClickListener()	{
//	
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					dialog.cancel();
//				}
//			});
//		// Show
//		ad.show();
	}
	
	private void showSelectUILanguageDialog() throws Exception	{
		AlertDialog.Builder ad = new AlertDialog.Builder(getActivity())
		.setTitle(R.string.prefs_general_group_server_language_desc)
		.setCancelable(true)
	// Add items
		.setSingleChoiceItems(AppManager.getLocaleManager().getSupportedUILangs(), 
				AppManager.getLocaleManager().getCurrentUILangIndex(), 
				new OnClickListener() {
		
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try {
					String newLang = AppManager.getLocaleManager().getSupportedUILangsIds()[which];
				
					if(newLang != generalSettings.getUILang())	{
						generalSettings.setUILang(newLang);
						setValues();
						AppManager.getLocaleManager().updateLocale();
						
						// Relaunch activity 
						getActivity().finish();
						startActivity(getActivity().getIntent());
						
					}	
				} catch (Exception e) {
					LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
					e.printStackTrace();
				}
				dialog.cancel();
			}
		})
	// Add buttons
		.setNegativeButton(R.string.dialog_cancel, new OnClickListener()	{

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
	// Show
	ad.show();
	}
}
